import { _decorator, Component, Node,tween,color,Sprite,Color } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LoadingMask')
export class LoadingMask extends Component {

    private bgSprite:Sprite=null;
    private inited:boolean=false;

    public static readonly defaultBgColor:Color=color(20,20,20,0);

    onLoad()
    {

    }

    init()
    {
        if(this.inited)
            return;
        this.bgSprite=this.node.getChildByName("bg").getComponent(Sprite);
        this.inited=true;
    }

    start() {

    }

    public show()
    {
        if(!this.inited)
            this.init();

        this.node.active=true;
        this.bgSprite.color=LoadingMask.defaultBgColor;
        tween(this.bgSprite.node)
            .to(0.2, {}, {
                onUpdate: (target, ratio: number) => {
                    this.bgSprite.color = color(20, 20, 20, 255 * ratio);
                }
            })
            .start();
    }

    public close()
    {
        tween(this.bgSprite.node)
            .to(0.2, {}, {
                onUpdate: (target, ratio: number) => {
                    this.bgSprite.color = color(20, 20, 20, 255 * (1-ratio));
                }
            })
            .call(()=>{
                this.node.active=false;
            })
            .start();
    }

    update(deltaTime: number) {
        
    }
}

